﻿@section Scripts {
    <script type="text/javascript">
        $(document).ready(function () 
        {
            $("#accept-friendrequest-dialog").dialog({
                //Elements graphiques
                autoOpen: false,
                resizable: false,
                height: 200,
                width: 350,
                show: { effect: 'drop', direction: "up" },
                modal: true,
                draggable: false,
                //Define buttons of the dialog
                buttons:[{
                    text:"Ok",
                    click: function() 
                    { 
                        accept_friendrequest($(this).attr('data-id')); //Call the function accept_friendrequest
                        $( this ).dialog( "close" ); //Close the dialog
                    }
                }]
            });

            $("#decline-friendrequest-dialog").dialog({
                //Elements graphiques
                autoOpen: false,
                resizable: false,
                height: 200,
                width: 350,
                show: { effect: 'drop', direction: "up" },
                modal: true,
                draggable: false,
                //Define buttons of the dialog
                buttons:[{
                    text:"Ok",
                    click: function() 
                    { 
                        decline_friendrequest($(this).attr('data-id')); //Call the function decline_friendrequest
                        $( this ).dialog( "close" ); //Close the dialog
                    }
                }]
            });
            initcontent();
       });

        function initcontent()
        {
            $('#answer-friendrequest-div').load('Friendship/Friendlist', function(){
                // manage delete button behavior
                $('.acceptfriendrequestbutton').click(function () {
                    $("#accept-friendrequest-dialog").attr('data-id', $(this).attr('data-id'));
                    $("#accept-friendrequest-dialog").text('You have a new friend !');
                    $("#accept-friendrequest-dialog").dialog('open');
                });

                // manage delete button behavior
                $('.declinefriendrequestbutton').click(function () {
                    $("#decline-friendrequest-dialog").attr('data-id', $(this).attr('data-id'));
                    $("#decline-friendrequest-dialog").text('You have decline this invitation.!');
                    $("#decline-friendrequest-dialog").dialog('open');
                });
            }
        }

        function accept_friendrequest(id) 
        {
            $.ajax({
                type: "POST",
                url: "Friendship/AcceptFriendship.cshtml/" + id,
            })
            .done(function (msg) {
                alert("You are now friends !");
                initcontent();
            });
        }

        function decline_friendrequest(id) 
        {
            $.ajax({
                type: "POST",
                url: "Friendship/DeclineFriendship.cshtml/" + id,
            })
            .done(function (msg) {
                alert("You declined this.");
                initcontent();
            });
        }
    </script>
}

@{
    Layout = "~/_SiteLayout.cshtml";
    Page.Title = "Profile";
}

<hgroup class="title">
    <h1>@Page.Title.</h1>
    <h2>Your Profile</h2>
</hgroup>


@if (WebSecurity.IsAuthenticated) {
    <text>
        Hello, <a class="email" href="~/Account/Manage" title="Manage">@WebSecurity.CurrentUserName</a> !
    </text> 
    
    @RenderPage("~/Friendship/FriendRequest.cshtml", null)
    
    <div id="accept-friendrequest-dialog"></div>
    <div id="decline-friendrequest-dialog"></div>
    
    <div id="answer-friendrequest-div"></div>

}
else
{
    Response.Redirect("Default");
}